<?php
if (!defined('INPOL')) return2();
$parse=$Lang;
$parse['page']=$page;
$parse['action']='edit';
$parse['empty_selected']='';
$parse['button_lang']=$Lang['save'];
$parse['button_click']='newPolicyPost();';
$parse['selects_disabled']='';
$parse['ins_comps']='';
$parse['nr_value']='';
$parse['nr_value']=$pol['nr'];
$parse['add_nr_value']=$pol['add_nr'];
$parse['pol_attr_checked']=$pol['client_attraction']=='Y'?' checked="checked"':'';
//$parse['pol_canc_checked']=$pol[''];
$parse['pol_canc_checked']='';
$parse['prev_pol_disabled']=' disabled="disabled"';
$parse['prev_pol_value']=$pol['prev_nr'];
$parse['cta_premium_value']=$pol['cta_premium'];
$parse['ng_premium_value']=$pol['ng_premium'];
$parse['riska_class_value']=$pol['octa_risks'];
$parse['issue_date_value']=$pol['issue_date'];
$parse['date_from_value']=$pol['from_date'];
$parse['date_from_time_value']=$pol['from_time'];
$parse['date_to_value']=$pol['to_date'];
$parse['payment_count_value']=$pol['payment_count'];
$parse['payment_none_selected']='';
$parse['payment_cash_selected']='';
$parse['payment_transfer_selected']='';
switch($pol['payment_type'])
{
    case '':
    case 0:
    case '0':
        $parse['payment_none_selected']=' selected="selected"';
    break;
    case '1':
    case 1:
        $parse['payment_cash_selected']=' selected="selected"';
    break;
    case '2':
    case 2:
        $parse['payment_transfer_selected']=' selected="selected"';
    break;
}
$parse['ins_sum_value']=$pol['ins_sum'];
$parse['ins_prem_value']=$pol['ins_prem'];
$parse['commision_perc_value']=$pol['comm_rate'];
$parse['broker_perc_value']=$pol['broker_rate'];
$parse['discount_perc_value']=$pol['add_disc_rate'];
$parse['comment_value']=$pol['comment'];
$parse['ins_type_update_check_db']='false';
$parse['payment_button']='<input type="button" class="yt_button" value="'.$Lang['view_payments'].'" onclick="openWindow(\'?p=payments&amp;a=view&amp;id='.$id.'\', \'Payments\', 200, 400);" />';
$parse['init_js']='initEditPolicy();';
$tqry=$sql->query("SELECT id, name, abbr FROM insurance_companies;");
while($otype=$sql->fetch_array($tqry))
{
    $sel=$otype['id']==$pol['ins_comp_id']?' selected="selected"':'';
    $parse['ins_comps'].='<option value="'.$otype['id'].'"'.$sel.'>'.$otype['abbr'].'</option>';
}
$parse['ins_types']='';
$parse['statuses']='';
$parse['takers']='';
$parse['owners']='';
$parse['subscribers']='';

//$parse['payment_button']='<font color="red">Payments button will be added soon.</font>';
$iqry=$sql->query("SELECT id, name FROM ins_type");
while($ins_type=$sql->fetch_array($iqry))
{
    $sel=$ins_type['id']==$pol['ins_type_id']?' selected="selected"':'';
    $parse['ins_types'].='<option value="'.$ins_type['id'].'"'.$sel.'>'.$ins_type['name'].'</option>';
}

$objects=$sql->query("SELECT objects.id as oId, object_type.id as tId, objects.loc, objects.cta_person, objects.`desc`, objects.reg_nr, objects.manufacturer, objects.model FROM pol_obj, objects, object_type WHERE pol_obj.oId = objects.id AND objects.tId = object_type.id AND pol_obj.pId = '$id' LIMIT 2;");
$i=1;
$obj_t1=0;
$obj_t2=0;
while($object=$sql->fetch_array($objects))
{
    $desc=($object['tId']!=getConfig('settings','transport_id', '1'))?''.$object['loc'].' | '.$object['cta_person'].' | '.$object['desc']:$object['reg_nr'].' | '.$object['manufacturer'].' | '.$object['model'];
    $parse['object'.$i.'']='<option value="'.$object['oId'].'" selected="selected">'.$desc.'</option>';
    if($i==1)$obj_t1=$object['tId'];else$obj_t2=$object['tId'];
    $parse['obj_types'.$i.'']='';
    $i++;
}
$obj_types=$sql->query("SELECT id, `name` FROM object_type;");

while($obj_type=$sql->fetch_array($obj_types))
{
    $sel=$obj_t1==$obj_type['id']?' selected="selected"':'';
    $parse['obj_types1'].='<option value="'.$obj_type['id'].'"'.$sel.'>'.$obj_type['name'].'</option>';
}
$sql->reset_query($obj_types);
while($obj_type=$sql->fetch_array($obj_types))
{
    $sel=$obj_t2==$obj_type['id']?' selected="selected"':'';
    $parse['obj_types2'].='<option value="'.$obj_type['id'].'"'.$sel.'>'.$obj_type['name'].'</option>';
}
$iqry=$sql->query("SELECT id, name FROM pol_status");
while($ins_type=$sql->fetch_array($iqry))
{
    $sel=$ins_type['id']==$pol['state_id']?' selected="selected"':'';
    $parse['statuses'].='<option value="'.$ins_type['id'].'"'.$sel.'>'.$ins_type['name'].'</option>';
}

$tqry=$sql->query("SELECT id, name, code, address FROM clients ORDER BY name ASC");
while($taker=$sql->fetch_array($tqry))
{
    $taker['name']=str_replace("&","&amp;", $taker['name']);
    $sel1=$taker['id']==$pol['taker_id']?' selected="selected"':'';
    $parse['takers'].='<option value="'.$taker['id'].'"'.$sel1.'>'.$taker['name'].' | '.$taker['code'].' | '.$taker['address'].'</option>';
    $sel2=$taker['id']==$pol['owner_id']?' selected="selected"':'';
    $parse['owners'].='<option value="'.$taker['id'].'"'.$sel2.'>'.$taker['name'].' | '.$taker['code'].' | '.$taker['address'].'</option>';
    $sel3=$taker['id']==$pol['represent_id']?' selected="selected"':'';
    $parse['subscribers'].='<option value="'.$taker['id'].'"'.$sel3.'>'.$taker['name'].' | '.$taker['code'].' | '.$taker['address'].'</option>';
}
$parse['tl_codes']='';
$octa_codes_qry=$sql->query("SELECT id, code, name FROM octa_codes");
while($tl_codes=$sql->fetch_array($octa_codes_qry))
{
    $sel=$pol['octa_code_id']==$tl_codes['id']?' selected="selected"':'';
    $parse['tl_codes'].='<option value="'.$tl_codes['id'].'"'.$sel.'>'.$tl_codes['code'].' | '.$tl_codes['name'].'</option>';
}

$parse['octa_examples']='';
$octa_exmpl_qry=$sql->query("SELECT id,code, name FROM octa_examples;");
while($exmpl=$sql->fetch_array($octa_exmpl_qry))
{
    $sel=$pol['octa_example_id']==$exmpl['id']?' selected="selected"':'';
    $parse['octa_examples'].= '<option value="'.$exmpl['id'].'"'.$sel.'>'.$exmpl['code'].' | '.$exmpl['name'].'</option>';
}
$parse['octa_discountss']='';
$octa_dsc_qry=$sql->query("SELECT id,code, name FROM octa_discounts;");
while($dsc=$sql->fetch_array())
{
    $sel=$pol['octa_discount_id']==$dsc['id']?' selected="selected"':'';
    $parse['octa_discountss'].= '<option value="'.$dsc['id'].'"'.$sel.'>'.$dsc['code'].' | '.$dsc['name'].'</option>';
}

$parse['val_territories']='';
$valtqry=$sql->query("SELECT id, name FROM validity_territory;");
while($terr=$sql->fetch_array($valtqry))
{
    $sel=$pol['validity_territory_id']==$terr['id']?' selected="selected"':'';
    $parse['val_territories'].= '<option value="'.$terr['id'].'"'.$sel.'>'.$terr['name'].'</option>';
}            
$parse['currencies']='';
$curr_qry=$sql->query("SELECT id, name FROM currency");
while($curr=$sql->fetch_array($curr_qry))
{
    $sel=$pol['currency_id']==$curr['id']?' selected="selected"':'';
    $parse['currencies'].= '<option value="'.$curr['id'].'"'.$sel.'>'.$curr['name'].'</option>';
}
$parse['brokers']='';
$brokers=$sql->query('SELECT id, name FROM brokers');
while($broker=$sql->fetch_array($brokers))
{
    $sel=$pol['broker_id']==$broker['id']?' selected="selected"':'';
    $parse['brokers'].= '<option value="'.$broker['id'].'"'.$sel.'>'.$broker['name'].'</option>';
}
$parse['employees']='';
$emp_qry=$sql->query('SELECT id, name, user FROM employees');
while($emp=$sql->fetch_array($emp_qry))
{
    
    $selected=$emp['id']==$pol['employee_id']?'selected="selected"':'';
    $parse['employees'].= '<option value="'.$emp['id'].'"'.$selected.'>'.$emp['name'].'</option>';
}
$tpl->parsetemplate('policies',$parse);
?>